package com.motu.vertx.module.utility.webhandler;




import com.motu.vertx.module.utility.mysql.MysqlTool;
import com.motu.vertx.module.utility.webtool.BaseWebHandler;
import io.vertx.core.json.JsonArray;
import io.vertx.core.json.JsonObject;
import io.vertx.ext.web.RoutingContext;
import io.vertx.ext.web.client.WebClient;
import io.vertx.sqlclient.Pool;

public class ActtypeHandler extends BaseWebHandler{
	public static final String tableName = "acttype";
	private static Pool jdbcClientCommon = null;

	public static void init(Pool jdbcCommon){
		jdbcClientCommon = jdbcCommon;
	}
	public static void list(RoutingContext routingContext,JsonObject json,String adminName,WebClient client){
		getTableList(routingContext, jdbcClientCommon,tableName);
	}
	public static void add(RoutingContext routingContext,JsonObject json,String adminName,WebClient client){
		addToTable(routingContext,jdbcClientCommon, tableName,null);
	}

	public static void update(RoutingContext routingContext,JsonObject json,String adminName,WebClient client){

		updateToTable(routingContext,jdbcClientCommon, tableName,null);
	}

	public static void getone(RoutingContext routingContext,JsonObject json,String adminName,WebClient client){
		getFromTable(routingContext,jdbcClientCommon, tableName);

	}
	public static void delete(RoutingContext routingContext,JsonObject json,String adminName,WebClient client){
		deleteFromTable(routingContext,jdbcClientCommon, tableName);
	}

	/*
	 * 导入 配置充值活动类型
	 */
	public static void importMore (RoutingContext routingContext,JsonObject json,String adminName,WebClient client){
		JsonArray list = json.getJsonArray("list");
		System.out.println(list);
		int max = list.size();
		String valueStr = "";
		for(int i=0; i<max; i++) {
			String childTmp = "";
			JsonObject listChild = list.getJsonObject(i);
			Integer idTmp =  listChild.getInteger("id");
			Integer typeTmp = listChild.getInteger("type");
			String nameTmp = listChild.getString("name");
			String desTmp = listChild.getString("des");
			String detaildesTmp = listChild.getString("detaildes");
			String fielddesTmp = listChild.getString("fielddes");
			String itemlabelTmp = listChild.getString("itemlabel");
			childTmp = "('" + idTmp + "','" + typeTmp + "','" + nameTmp + "','" + desTmp + "','" + detaildesTmp + "','" + fielddesTmp + "','" + itemlabelTmp +"')";
			valueStr += childTmp;
			if(i < max-1) {
				valueStr += ","; 
			}
		}
		String sql = "INSERT IGNORE INTO " + tableName + " (`id`, `type`, `name`, `des`, `detaildes`, `fielddes`, `itemlabel`) VALUES " + valueStr;
		JsonArray params = new JsonArray();
		MysqlTool.insertAndGetID(jdbcClientCommon, sql, params, handler->{
			if(handler.succeeded()){
				sendHttpResponse(routingContext.response(), new JsonObject());
			}else{
				sendHttpResponseError(routingContext.response(), handler.cause().getMessage());
			}
		});
	}


}
